﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeltaFlex.Data;
using System.Data;
using Taula = DeltaFlex.Negoci.ConsTaules.ClientArticlePreu;

namespace DeltaFlex.Negoci
{
    [Serializable()]
   public class ClientArticlePreu
    {
        #region Variables
        private int _idClient;
        private int _idArticle;
        private decimal _preu;
        #endregion

        #region Atributs
        public int IdClient
        {
            get { return _idClient; }
            set { _idClient = value; }

        }

        public int IdArticle
        {
            get { return _idArticle; }
            set { _idArticle = value; }
        }

        public decimal Preu
        {
            get { return _preu; }
            set { _preu = value; }
        }

       #endregion

        #region Inserts
        public static int InsertarClientArticlePreu(int idClient, int idArticle, decimal preu)
        {
            try
            {
                return Data.ClientArticlePreu.InsertarClientArticlePreu(idClient, idArticle, preu);
            }
            catch { throw; }

        }

        #endregion

        #region Selects
       /// <summary>
        /// Recupera des de BD la relacio ClientArticlePreu
       /// </summary>
       /// <param name="idClient">El Client sobre el qual es volen saber els preus dels articles</param>
       /// <param name="idArticle">Si aquest parametre rep un 0, es cerquen tonts els articles per al idClient rebut, en cas contrari, 
       ///                          l'article desitjat</param>
        /// <returns>Recupera des de BD la relacio ClientArticlePreu</returns>
        public static List<ClientArticlePreu> LlistarClientArticlePreu_ByClientArticle(int idClient, int idArticle)
        {
            List<ClientArticlePreu> lstCAP= new List<ClientArticlePreu>();
            IDataReader reader = null;

            try
            {
                reader = Data.ClientArticlePreu.CercarClientArticlePreu_ByClientArticle(idClient,idArticle);

                while (reader.Read())
                {
                    lstCAP.Add(MapEntityFromDataReader(reader));
                }
            }
            catch { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }

            return lstCAP;
        }
        #endregion

        #region Mapeig
        public static ClientArticlePreu MapEntityFromDataReader(IDataReader record)
        {
            ClientArticlePreu cap = new ClientArticlePreu();

            MemberAllocation.Allocate(record, Taula.COLUMNA_IDCLIENT, ref cap._idClient);
            MemberAllocation.Allocate(record, Taula.COLUMNA_IDARTICLE, ref cap._idArticle);
            MemberAllocation.Allocate(record, Taula.COLUMNA_PREU, ref cap._preu);
            
            return cap;
        }
        #endregion
    }
}
